<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Norms</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 4.1.0">
<link rel="up" href="../vector_functionals.html" title="Chapter 7. Vector Functionals - Norms">
<link rel="prev" href="../vector_functionals.html" title="Chapter 7. Vector Functionals - Norms">
<link rel="next" href="../special.html" title="Chapter 8. Special Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../vector_functionals.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../vector_functionals.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../special.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.norms"></a><a class="link" href="norms.html" title="Norms">Norms</a>
</h2></div></div></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        All of the functions below require at least C++17
      </p></td></tr>
</table></div>
<h4>
<a name="math_toolkit.norms.h0"></a>
      <span class="phrase"><a name="math_toolkit.norms.synopsis"></a></span><a class="link" href="norms.html#math_toolkit.norms.synopsis">Synopsis</a>
    </h4>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">norms</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">tools</span> <span class="special">{</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l0_pseudo_norm</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l0_pseudo_norm</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="identifier">size_t</span> <span class="identifier">hamming_distance</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">first2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="identifier">size_t</span> <span class="identifier">hamming_distance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l1_norm</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l1_norm</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l1_distance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l1_distance</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">first2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l2_norm</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l2_norm</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l2_distance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">l2_distance</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">first2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">sup_norm</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">sup_norm</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">sup_distance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">sup_distance</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">first2</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">lp_norm</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">p</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">lp_norm</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">p</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">lp_distance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v1</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v2</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">p</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">lp_distance</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">first2</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">p</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">total_variation</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="identifier">total_variation</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">last</span><span class="special">);</span>

<span class="special">}}}</span>
</pre>
<h4>
<a name="math_toolkit.norms.h1"></a>
      <span class="phrase"><a name="math_toolkit.norms.description"></a></span><a class="link" href="norms.html#math_toolkit.norms.description">Description</a>
    </h4>
<p>
      The file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">norms</span><span class="special">.</span><span class="identifier">hpp</span></code> is a set of facilities for computing
      scalar values traditionally useful in numerical analysis from vectors.
    </p>
<p>
      Our examples use <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span></code> to
      hold the data, but this not required. In general, you can store your data in
      an Eigen array, an Armadillo vector, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span></code>,
      and for many of the routines, a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward_list</span></code>.
      These routines are usable in float, double, long double, and Boost.Multiprecision
      precision, as well as their complex extensions whenever the computation is
      well-defined. Integral datatypes are supported for most routines.
    </p>
<h4>
<a name="math_toolkit.norms.h2"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_u221e_norm"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_u221e_norm">ℓ<sup>∞</sup> norm</a>
    </h4>
<p>
      Computes the supremum norm of a dataset:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{-</span><span class="number">3</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">sup</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">sup_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">cend</span><span class="special">());</span>
<span class="comment">// sup = 3</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&gt;</span> <span class="identifier">v</span><span class="special">{{</span><span class="number">0</span><span class="special">,</span> <span class="special">-</span><span class="number">8</span><span class="special">},</span> <span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">},</span> <span class="special">{-</span><span class="number">3</span><span class="special">,</span><span class="number">2</span><span class="special">}};</span>
<span class="comment">// Range call:</span>
<span class="keyword">double</span> <span class="identifier">sup</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">sup_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
<span class="comment">// sup = 8</span>
</pre>
<p>
      Supports real, integral, and complex arithmetic. Container must be forward
      iterable and is not modified.
    </p>
<h4>
<a name="math_toolkit.norms.h3"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_u221e_distance"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_u221e_distance">ℓ<sup>∞</sup> distance</a>
    </h4>
<p>
      Computes the supremum norm distance between two vectors:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{-</span><span class="number">3</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">1</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">{</span><span class="number">6</span><span class="special">,</span> <span class="special">-</span><span class="number">2</span><span class="special">,</span> <span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">sup</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">sup_distance</span><span class="special">(</span><span class="identifier">w</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
<span class="comment">// sup = 9</span>
</pre>
<p>
      Supports real, integral, and complex arithmetic. Container must be forward
      iterable and is not modified. If the input it integral, the output is a double
      precision float.
    </p>
<h4>
<a name="math_toolkit.norms.h4"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_p_norm"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_p_norm">ℓ<sup><span class="emphasis"><em>p</em></span></sup> norm</a>
    </h4>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{-</span><span class="number">8</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">sup</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">lp_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">cend</span><span class="special">(),</span> <span class="number">7</span><span class="special">);</span>
<span class="comment">// sup = 8</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&gt;</span> <span class="identifier">v</span><span class="special">{{</span><span class="number">1</span><span class="special">,</span> <span class="number">0</span><span class="special">},</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">},</span> <span class="special">{</span><span class="number">0</span><span class="special">,-</span><span class="number">1</span><span class="special">}};</span>
<span class="keyword">double</span> <span class="identifier">sup</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">lp_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">cend</span><span class="special">(),</span> <span class="number">3</span><span class="special">);</span>
<span class="comment">// sup = cbrt(3)</span>
</pre>
<p>
      Supports both real, integral, and complex arithmetic. If the input is integral,
      the output is a double precision float. The container must be forward iterable
      and the contents are not modified.
    </p>
<p>
      Only supports integral <span class="emphasis"><em>p</em></span> for two reasons: The computation
      is much slower for real <span class="emphasis"><em>p</em></span>, and the non-integral ℓ<sup><span class="emphasis"><em>p</em></span></sup> norm
      is rarely used.
    </p>
<h4>
<a name="math_toolkit.norms.h5"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_p_distance"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_p_distance">ℓ<sup><span class="emphasis"><em>p</em></span></sup> distance</a>
    </h4>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{-</span><span class="number">8</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">{</span><span class="number">8</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">dist</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">lp_distance</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">w</span><span class="special">,</span> <span class="number">7</span><span class="special">);</span>
<span class="comment">// dist = 16</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&gt;</span> <span class="identifier">v</span><span class="special">{{</span><span class="number">1</span><span class="special">,</span> <span class="number">0</span><span class="special">},</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">},</span> <span class="special">{</span><span class="number">0</span><span class="special">,-</span><span class="number">1</span><span class="special">}};</span>
<span class="keyword">double</span> <span class="identifier">dist</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">lp_distance</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">v</span><span class="special">,</span> <span class="number">3</span><span class="special">);</span>
<span class="comment">// dist = 0</span>
</pre>
<p>
      Supports both real, integral, and complex arithmetic. If the input is integral,
      the output is a double precision float. The container must be forward iterable
      and the contents are not modified.
    </p>
<p>
      Only supports integer <span class="emphasis"><em>p</em></span>.
    </p>
<h4>
<a name="math_toolkit.norms.h6"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_0_pseudo_norm"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_0_pseudo_norm">ℓ<sup>0</sup> pseudo-norm</a>
    </h4>
<p>
      Counts the number of non-zero elements in a container.
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="identifier">size_t</span> <span class="identifier">count</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">l0_pseudo_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
<span class="comment">// count = 1</span>
</pre>
<p>
      Supports real, integral, and complex numbers. The container must be forward
      iterable and the contents are not modified. Note that this measure is not robust
      against numerical noise and is therefore not as useful as (say) the Hoyer sparsity
      in numerical applications. Works with real, complex, and integral inputs.
    </p>
<h4>
<a name="math_toolkit.norms.h7"></a>
      <span class="phrase"><a name="math_toolkit.norms.hamming_distance"></a></span><a class="link" href="norms.html#math_toolkit.norms.hamming_distance">Hamming
      Distance</a>
    </h4>
<p>
      Compute the number of non-equal elements between two vectors <span class="emphasis"><em>w</em></span>
      and <span class="emphasis"><em>v</em></span>:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">0</span><span class="special">,</span><span class="number">0</span><span class="special">};</span>
<span class="identifier">size_t</span> <span class="identifier">count</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">hamming_distance</span><span class="special">(</span><span class="identifier">w</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
<span class="comment">// count = 2</span>
</pre>
<p>
      Works for any datatype for which the operator <code class="computeroutput"><span class="special">!=</span></code>
      is defined.
    </p>
<h4>
<a name="math_toolkit.norms.h8"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_1_norm"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_1_norm">ℓ<sup>1</sup> norm</a>
    </h4>
<p>
      The ℓ<sup>1</sup> norm is a special case of the ℓ<sup><span class="emphasis"><em>p</em></span></sup> norm,
      but is much faster:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">l1</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">l1_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
<span class="comment">// l1 = 3</span>
</pre>
<p>
      Requires a forward iterable input, does not modify input data, and works with
      real, integral, and complex numbers.
    </p>
<h4>
<a name="math_toolkit.norms.h9"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_1_distance"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_1_distance">ℓ<sup>1</sup> distance</a>
    </h4>
<p>
      Computes the ℓ<sup>1</sup> distance between two vectors:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">dist</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">l1_distance</span><span class="special">(</span><span class="identifier">w</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
<span class="comment">// dist = 0</span>
</pre>
<p>
      Requires a forward iterable inputs, does not modify input data, and works with
      real, integral, and complex numbers. If the input type is integral, the output
      is a double precision float.
    </p>
<h4>
<a name="math_toolkit.norms.h10"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_2_norm"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_2_norm">ℓ<sup>2</sup> norm</a>
    </h4>
<p>
      The ℓ<sup>2</sup> norm is again a special case of the ℓ<sup><span class="emphasis"><em>p</em></span></sup> norm,
      but is much faster:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">l2</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">l2_norm</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
<span class="comment">// l2 = sqrt(3)</span>
</pre>
<p>
      Requires a forward iterable input, does not modify input data, and works with
      real, complex and integral data. If the input is integral, the output is a
      double precision float.
    </p>
<h4>
<a name="math_toolkit.norms.h11"></a>
      <span class="phrase"><a name="math_toolkit.norms.u2113_super_2_distance"></a></span><a class="link" href="norms.html#math_toolkit.norms.u2113_super_2_distance">ℓ<sup>2</sup> distance</a>
    </h4>
<p>
      Compute the ℓ<sup>2</sup> distance between two vectors <span class="emphasis"><em>w</em></span> and
      <span class="emphasis"><em>v</em></span>:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">dist</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">l2_distance</span><span class="special">(</span><span class="identifier">w</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
<span class="comment">// dist = 1</span>
</pre>
<p>
      Requires a forward iterable input, does not modify input data, and works with
      real, complex numbers, and integral data. If the input type is integral, the
      output is a double precision float.
    </p>
<h4>
<a name="math_toolkit.norms.h12"></a>
      <span class="phrase"><a name="math_toolkit.norms.total_variation"></a></span><a class="link" href="norms.html#math_toolkit.norms.total_variation">Total
      Variation</a>
    </h4>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">tv</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">total_variation</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
<span class="comment">// no variation in v, so tv = 0.</span>
<span class="identifier">v</span> <span class="special">=</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">tv</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">total_variation</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
<span class="comment">// variation is 1, so tv = 1.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
<span class="keyword">double</span> <span class="identifier">tv</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">::</span><span class="identifier">total_variation</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
</pre>
<p>
      The total variation only supports real numbers and integers. If the input is
      integral, the output is a double precision float.
    </p>
<p>
      All the constituent operations to compute the total variation are well-defined
      for complex numbers, but the computed result is not meaningful; a 2D total
      variation is more appropriate. The container must be forward iterable, and
      the contents are not modified.
    </p>
<p>
      As an aside, the total variation is not technically a norm, since <span class="emphasis"><em>TV(v)
      = 0</em></span> does not imply <span class="emphasis"><em>v = 0</em></span>. However, it satisfies
      the triangle inequality and is absolutely 1-homogeneous, so it is a seminorm,
      and hence is grouped with the other norms here.
    </p>
<h4>
<a name="math_toolkit.norms.h13"></a>
      <span class="phrase"><a name="math_toolkit.norms.references"></a></span><a class="link" href="norms.html#math_toolkit.norms.references">References</a>
    </h4>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Higham, Nicholas J. <span class="emphasis"><em>Accuracy and stability of numerical algorithms.</em></span>
          Vol. 80. Siam, 2002.
        </li>
<li class="listitem">
          Mallat, Stephane. <span class="emphasis"><em>A wavelet tour of signal processing: the sparse
          way.</em></span> Academic press, 2008.
        </li>
<li class="listitem">
          Hurley, Niall, and Scott Rickard. <span class="emphasis"><em>Comparing measures of sparsity.</em></span>
          IEEE Transactions on Information Theory 55.10 (2009): 4723-4741.
        </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../vector_functionals.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../vector_functionals.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../special.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
